package com.hxit.huaxiamelongarden.service.impl;

import com.hxit.huaxiamelongarden.domin.dto.IndexDto;
import com.hxit.huaxiamelongarden.domin.po.Content;
import com.hxit.huaxiamelongarden.domin.po.Tags;
import com.hxit.huaxiamelongarden.domin.po.User;
import com.hxit.huaxiamelongarden.domin.vo.IndexVo;
import com.hxit.huaxiamelongarden.domin.vo.UserVo;
import com.hxit.huaxiamelongarden.service.ContentService;
import com.hxit.huaxiamelongarden.service.IndexService;
import com.hxit.huaxiamelongarden.service.TagService;
import com.hxit.huaxiamelongarden.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName IndexServiceImpl
 * @description 首页逻辑实现类
 * @Author 12694
 * @Date 2025/1/28 16:36
 * @Version 1.0
 **/
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private ContentService contentService;
    @Autowired
    private UserService userService;
    @Autowired
    private TagService tagService;

    //获取所有帖子
    @Override
    public List<IndexVo> list() {
        List<Content> allContent = contentService.getAllContent();
        List<IndexVo> indexVoList = allContent.stream().map(content -> {
            User user = new User();
            UserVo userVo = userService.selectById(content.getUserId());
            user.setNickname(userVo.getNickname());
            user.setImage(userVo.getImage());
            String nickName = user.getNickname();
            String image = user.getImage();
            List<Tags> tagsList = tagService.findTagByContentId(content.getContentId());
            IndexVo indexVo = new IndexVo();
            indexVo.setNickname(nickName);
            indexVo.setImage(image);
            indexVo.setTagsList(tagsList);
            indexVo.setContent(content);
            return indexVo;
        }).collect(Collectors.toList());
        return indexVoList;
    }

    //根据用户搜索获取帖子
    @Override
    public List<IndexVo> searchByUser(IndexDto indexDto) {
        UserVo userVo = userService.selectByLike(indexDto);
        List<Content> contentList = contentService.getCommentsByUserId(userVo.getUserId());
        List<IndexVo> indexVoList = contentList.stream().map(content -> {
            String nickName = userVo.getNickname();
            String image = userVo.getImage();
            IndexVo indexVo = new IndexVo();
            indexVo.setNickname(nickName);
            indexVo.setImage(image);
            indexVo.setContent(content);
            List<Tags> tagsList = tagService.findTagByContentId(content.getContentId());
            indexVo.setTagsList(tagsList);
            return indexVo;
        }).collect(Collectors.toList());
        return indexVoList;
    }

    //根据话题或者内容获取信息
    @Override
    public List<IndexVo> searchByTitleOrContent(IndexDto indexDto) {
        List<Content> contentList = contentService.getSearchContent(indexDto);
        List<IndexVo> indexVoList = contentList.stream().map(content -> {
            User user = new User();
            UserVo userVo = userService.selectById(content.getUserId());
            user.setNickname(userVo.getNickname());
            user.setImage(userVo.getImage());
            String nickName = user.getNickname();
            String image = user.getImage();
            IndexVo indexVo = new IndexVo();
            indexVo.setContent(content);
            indexVo.setNickname(nickName);
            indexVo.setImage(image);
            List<Tags> tagsList = tagService.findTagByContentId(content.getContentId());
            indexVo.setTagsList(tagsList);
            return indexVo;
        }).collect(Collectors.toList());
        return indexVoList;
    }

    //根据标签获取信息
    @Override
    public List<IndexVo> searchByTags(IndexDto indexDto) {
        List<Content> contentList = contentService.findTagByTagName(indexDto);
        List<IndexVo> indexVoList = contentList.stream().map(content -> {
            User user = new User();
            UserVo userVo = userService.selectById(content.getUserId());
            user.setNickname(userVo.getNickname());
            user.setImage(userVo.getImage());
            String nickName = user.getNickname();
            String image = user.getImage();
            IndexVo indexVo = new IndexVo();
            indexVo.setContent(content);
            indexVo.setNickname(nickName);
            indexVo.setImage(image);
            List<Tags> tagsList = tagService.findTagByContentId(content.getContentId());
            indexVo.setTagsList(tagsList);
            return indexVo;
        }).collect(Collectors.toList());
        return indexVoList;
    }
}